package cn.flying.base.process.configuration;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.base.process.listener.eventlistener.ActivityCancelledListener;
import cn.flying.base.process.listener.eventlistener.ActivityCompletedListener;
import cn.flying.base.process.listener.eventlistener.ActivitySignaledListener;
import cn.flying.base.process.listener.eventlistener.ActivityStartedListener;
import cn.flying.base.process.listener.eventlistener.GlobalEventListener;
import cn.flying.base.process.listener.eventlistener.JobExecutionFailureListener;
import cn.flying.base.process.listener.eventlistener.ProcessCancelledListener;
import cn.flying.base.process.listener.eventlistener.ProcessCompletedListener;
import cn.flying.base.process.listener.eventlistener.ProcessStartedListener;
import cn.flying.base.process.listener.eventlistener.TaskAssignedListener;
import cn.flying.base.process.listener.eventlistener.TaskCompletedListener;
import cn.flying.base.process.listener.eventlistener.TaskCreatedListener;


/**
 * @description: 工作流自定义配置
 * @author: lvyong
 * @date: 2023年02月20日 15:58
 * @version: 1.0
 */
@Component
public class ActivityConfiguration implements ProcessEngineConfigurationConfigurer {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private GlobalEventListener eventListener;
    @Resource
    private JobExecutionFailureListener jobExecutionFailureListener;
    @Resource
    private ActivityCancelledListener activityCancelledListener;
    @Resource
    private ActivityCompletedListener activityCompletedListener;
    @Resource
    private ActivitySignaledListener activitySignaledListener;
    @Resource
    private ActivityStartedListener activityStartedListener;
    @Resource
    private ProcessCancelledListener processCancelledListener;
    @Resource
    private ProcessCompletedListener processCompletedListener;
    @Resource
    private ProcessStartedListener processStartedListener;
    @Resource
    private TaskAssignedListener taskAssignedListener;
    @Resource
    private TaskCompletedListener taskCompletedListener;
    @Resource
    private TaskCreatedListener taskCreatedListener;

    @Override
    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
        logger.info("activiti工作流个性化配置");
        List<ActivitiEventListener> listeners = new ArrayList<>();
        listeners.add(eventListener);
        processEngineConfiguration.setEventListeners(listeners);

        //添加执行失败监听
        Map<String, List<ActivitiEventListener>> typedListeners = new HashMap<>();
        List<ActivitiEventListener> execFailListeners = new ArrayList<>();
        execFailListeners.add(jobExecutionFailureListener);
        typedListeners.put(ActivitiEventType.JOB_EXECUTION_FAILURE.name(), execFailListeners);

        //流程开始监听
        listeners = new ArrayList<>();
        listeners.add(processStartedListener);
        typedListeners.put(ActivitiEventType.PROCESS_STARTED.name(), listeners);

        //流程结束监听
        listeners = new ArrayList<>();
        listeners.add(processCompletedListener);
        typedListeners.put(ActivitiEventType.PROCESS_COMPLETED.name(), listeners);

        //流程取消监听
        listeners = new ArrayList<>();
        listeners.add(processCancelledListener);
        typedListeners.put(ActivitiEventType.PROCESS_CANCELLED.name(), listeners);

        //活动开始监听
        listeners = new ArrayList<>();
        listeners.add(activityStartedListener);
        typedListeners.put(ActivitiEventType.ACTIVITY_STARTED.name(), listeners);

        //活动结束监听
        listeners = new ArrayList<>();
        listeners.add(activityCompletedListener);
        typedListeners.put(ActivitiEventType.ACTIVITY_COMPLETED.name(), listeners);

        //活动取消监听
        listeners = new ArrayList<>();
        listeners.add(activityCancelledListener);
        typedListeners.put(ActivitiEventType.ACTIVITY_CANCELLED.name(), listeners);

        //活动信号触发监听
        listeners = new ArrayList<>();
        listeners.add(activitySignaledListener);
        typedListeners.put(ActivitiEventType.ACTIVITY_SIGNALED.name(), listeners);

        //任务创建监听
        listeners = new ArrayList<>();
        listeners.add(taskCreatedListener);
        typedListeners.put(ActivitiEventType.TASK_CREATED.name(), listeners);

        //任务完成监听
        listeners = new ArrayList<>();
        listeners.add(taskCompletedListener);
        typedListeners.put(ActivitiEventType.TASK_COMPLETED.name(), listeners);

        //任务分配监听
        listeners = new ArrayList<>();
        listeners.add(taskAssignedListener);
        typedListeners.put(ActivitiEventType.TASK_ASSIGNED.name(), listeners);

        processEngineConfiguration.setTypedEventListeners(typedListeners);
    }
}
